import socket
import urllib.request
import urllib.parse

response = urllib.request.urlopen('https://zh-hans.reactjs.org/')

header = dict(response.getheaders())
print(response.status)
print(header)
print(header.get('Server'))

data = bytes(urllib.parse.urlencode({ 'word': 'CSC' }).encode('utf8'))

try:
    cscResponse = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)    
except urllib.error.URLError as e:
    if isinstance(e.reason, socket.timeout):
        print('TIME OUT')

url = 'http://httpbin.org/post'
headers = {
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    'host': 'httpbin.org'
}
formData = {
    'name': 'Germey'
}
data = bytes(urllib.parse.urlencode(formData), 'utf-8')
req = urllib.request.Request(url=url, data=data, headers=headers, method='POST')
response = urllib.request.urlopen(req)
print(response.read().decode('utf8'))
